import Vue from 'vue'
import VueRouter from 'vue-router'
import { Toast } from 'vant'


import { getToken} from '@/utils/storage'



import Layout from '@/views/layout.vue'
import Find from '@/views/find.vue'
import Home from '@/views/home.vue'
import Information from '@/views/information.vue'
import User from '@/views/user.vue'
import Login from '@/views/login.vue'
import Register from '@/views/register.vue'
import Maps from '@/views/maps.vue'
import Location from '@/views/location.vue'
import Collect from '@/views/mycollect.vue'
import MyUser from '@/views/myuser'
import Detail from '@/views/detail.vue'
Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    component: Layout,
    redirect:'/home',
  },
  {
    path: '/login',
    component: Login,
  },
  {
    path: '/maps',
    component: Maps,
  },
  {
    path: '/location',
    component: Location,
  },
  {
    path: '/register',
    component: Register,
  },
  {
    path: '/mycollect',
    component: Collect,
  },
  {
    path: '/detail/:id',
    component: Detail,
  },
  {
    path: '/layout',
    component: Layout,
    redirect:'/home',
    children: [
      {
        path: '/find',
        component:Find,
      },
      {
        path: '/home',
        component:Home,
      },
      {
        path: '/information',
        component:Information,
      },
      {
        path: '/user',
        component:User,
      },
      {
        path: "/myuser",
        component: MyUser,
      },
    ]
  }

]

const router = new VueRouter({
  routes
})

// 新建白名单
const whiteList = ['/login', '/register', '/user']
router.beforeEach((to, from, next) => { 
  if (getToken()) {
    next()
  } else { 
    if (whiteList.includes(to.path)) {
      next()
    } else { 
      Toast.fail('游客请登录！')
      next('/login')
    }
  }
})
export default router
